package com.yunhe.option.controller.base;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.option.domain.base.FileResult;
import com.yunhe.option.service.base.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName FileUplaodController
 * @Description TODO
 * @Author HeTao
 * @Date 2021/3/2 16:55
 * @Version 1.0
 **/
@Api(value = "FileUploadController", description = "文件上传", tags = {"Base_FileUpload"})
@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 文件上传
     */
    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    @ApiOperation(value = "文件上传")
    public ResponseEntity<ResultObject> uploadFile(@RequestParam("file") MultipartFile file) throws IOException, ArgumentErrorException {
        FileResult fileResult = fileUploadService.uploadFile(file);
        return new ResponseEntity<>(new ResultObject<>(fileResult), HttpStatus.OK);
    }

    @RequestMapping(value = "/getFile",method = RequestMethod.GET)
    @ApiOperation(value = "文件流获取（下载）")
    public ResponseEntity<ResultObject> getFile(HttpServletRequest request,
                                                HttpServletResponse response,
                                                @RequestParam("filePath") String filePath) throws IOException, ArgumentErrorException {
        fileUploadService.getFile(filePath,response,request);
        return null;
    }

    @RequestMapping(value = "/deleteFile",method = RequestMethod.POST)
    @ApiOperation(value = "文件删除")
    public ResponseEntity<ResultObject> deleteFile(@RequestParam("filePath") String filePath) throws IOException, ArgumentErrorException {
        fileUploadService.deleteFile(filePath);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }
}
